package com.atguigu.gulimall.product.sync;
/**
 * 演示死锁
 *
 * */
public class DeadLock {
    private static Object o1 = new Object();
    private static Object o2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName()+"持有锁A,试图获取锁B");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"获取锁B");
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName()+"持有锁B,试图获取锁A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName()+"获取锁A");
                }
            }
        }, "B").start();
    }
}
